import Popup from '../event/popup'
import { hachureLines } from '../render/hachure'
import { isHoliday } from '../const/holidays'
import { addControls } from '../controls'
import { syncLocal, getRandomColor, getLocal, initData, updateData } from '../utils'
import { createFlagGroup } from '../event/flag'
import { getLeftHandleBar, getRightHandleBar, getRealDuration, getTaskBarMoveLine, createLeftArrowRect, createRightArrowRect } from '../task'
import { drawTodayLine } from '../event/today'
import { defaultTaskOwner, unitWidth, halfUnitWidth, taskNamePaddingLeft, initChartStartX, initChartStartY, timeScaleHeight, milestoneTopHeight, barHeight, barMargin, scrollSpeed, includeHoliday, mockTaskSize, todayOffset, $lastScrollXSpan, currentGroup, setCurrentGroup, initLastScrollX, filter } from '../config'
import ContextMenu from '../event/contextMenu'

/**
 * 初始化ZRender实例
 */
export function initializeZRender() {
  const zr = zrender.init(document.getElementById("zrender-container"), {
    renderer: 'canvas'
  });
  return zr;
}

/**
 * 初始化任务数据
 * @returns {Array} 任务数组
 */
export function initializeTasks() {
  const tasks = [
    { name: "Task 1", start: todayOffset + 0, duration: 3, resource: "John", fillColor: getRandomColor() },
    { name: "Task 2", start: todayOffset + 2, duration: 4, resource: "Jane", fillColor: getRandomColor() },
    { name: "Task 3 long long long", start: todayOffset + 7, duration: 1, resource: "Bob", fillColor: getRandomColor() },
    { name: "Task 4", start: todayOffset + 8, duration: 2, resource: "Bose", fillColor: getRandomColor() },
    { name: "Task 5", start: todayOffset + 10, duration: 3, resource: "Uno", fillColor: getRandomColor() },
    {}
  ];

  const lastTask = tasks.pop();
  while(tasks.length > 1 && tasks.length < mockTaskSize) {
    tasks.push(...tasks.map(item => ({...item})));
  }
  tasks.push(lastTask);

  return tasks;
}

/**
 * 初始化里程碑数据
 * @returns {Array} 里程碑数组
 */
export function initializeMilestones() {
  return [
    { start: 10, name: '提测' }
  ];
}

/**
 * 初始化滚动位置
 * @returns {Object} 滚动位置对象
 */
export function initializeScrollPosition() {
  return {
    lastScrollX: 0 + initLastScrollX,
    lastScrollY: 0
  };
}

/**
 * 应用过滤器
 * @param {Array} tasks 任务数组
 */
export function applyFilter(tasks) {
  if (filter) {
    updateData('tasks', tasks.filter(item => item.fillColor === filter));
  }
}

// 导出所有依赖，供其他模块使用
export {
  Popup,
  hachureLines,
  isHoliday,
  addControls,
  syncLocal,
  getRandomColor,
  getLocal,
  initData,
  updateData,
  createFlagGroup,
  getLeftHandleBar,
  getRightHandleBar,
  getRealDuration,
  getTaskBarMoveLine,
  createLeftArrowRect,
  createRightArrowRect,
  drawTodayLine,
  unitWidth,
  halfUnitWidth,
  taskNamePaddingLeft,
  initChartStartX,
  initChartStartY,
  timeScaleHeight,
  milestoneTopHeight,
  barHeight,
  barMargin,
  scrollSpeed,
  includeHoliday,
  mockTaskSize,
  todayOffset,
  $lastScrollXSpan,
  currentGroup,
  setCurrentGroup,
  initLastScrollX,
  filter,
  ContextMenu
}; 